{include file="public/header" /}
<div id="app">

        <el-row :gutter="20">
            <el-col :span="16">
                <el-form @submit.native.prevent :inline="true" class="demo-form-inline">
                    <el-form-item label="{:lang('test_title')}">
                        <el-input v-model="searchForm.title" placeholder="{:lang('test_title')}"></el-input>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="search">{:lang('search')}</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
            <el-col :span="8">
                <el-button style="float: right" type="success" icon="el-icon-plus" @click="showAdd">{:lang('add')}</el-button>
            </el-col>
        </el-row>

        <el-table
                @row-dblclick="dbclick"
                :data="tableData"
                border
                size="medium"
                style="width: 100%">
            <el-table-column
                    prop="add_time"
                    label="{:lang('test_date')}"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="title"
                    label="{:lang('test_title')}"
                    >
            </el-table-column>

            <el-table-column
                    align="center"
                    fixed="right"
                    label="{:lang('operation')}"
                    width="100">
                <template slot-scope="scope">
                    <el-link type="primary" @click="one(scope)">{:lang('edit')}</el-link>
                    <el-link type="danger" @click="del(scope)">{:lang('delete')}</el-link>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                background
                layout="prev, pager, next"
                :total="total" :page-size="limit"
                @current-change="change"
        >
        </el-pagination>

    <el-dialog :title="dialogFormTitle" :visible.sync="dialogFormVisible">
        <el-form @submit.native.prevent>
            <el-form-item label="{:lang('test_title')}" >
                <el-input v-model="form.title" autocomplete="off"></el-input>
            </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">{:lang('cancel')}</el-button>
            <el-button type="primary" :loading="loadBtn" @click="confirm">{:lang('confirm')}</el-button>
        </div>
    </el-dialog>
</div>
<script src="/static/js/http.js"></script>
<script>
    const url = "/manager/testList"
    var app1 = new Vue({
        el: '#app',
        data: {
            tableData: [],
            total: 4,
            limit: 10,
            page: 1,
            searchForm: {
                title: '',
            },
            dialogFormVisible: false,
            form: {
                title: '',
                test_id: ''
            },
            dialogFormTitle: '',
            loadBtn: false,
        },
        created() {
            this.testList()
        },
        methods: {
            search(){
                this.testList()
            },
            change(page){
                this.page = page
                this.testList()
            },
            testList() {
                http.post(url,{
                    page: this.page,
                    limit: this.limit,
                    title: this.searchForm.title,
                    ccOption: 'list'
                }).then(res => {
                    let d = res.data.data
                    this.size = d.limit
                    this.total = d.total
                    this.tableData = d.items
                })
            },
            showAdd() {
                this.resetForm()
                this.dialogFormTitle = "{:lang('add')}",
                this.dialogFormVisible = true
            },
            confirm() {
                if(this.form.test_id == '')
                {
                    this.add()
                }
                else
                {
                    this.edit()
                }
            },
            add() {
                this.loadBtn = true
                let newFormData = {...this.form,ccOption: 'add'}
                http.post(url,newFormData).then(res => {
                    this.loadBtn = false
                    if(res.data.code == 0)
                    {
                        this.$message.success("{:lang('operation_success')}");
                        this.dialogFormVisible = false;
                        this.testList()
                    }
                    else
                    {
                        this.$message.error(res.data.message);
                    }

                })
            },
            edit() {
                this.loadBtn = true
                let newFormData = {...this.form,ccOption: 'edit'}
                http.post(url,newFormData).then(res => {
                    this.loadBtn = false
                    if(res.data.code == 0)
                    {
                        this.$message.success("{:lang('operation_success')}");
                        this.dialogFormVisible = false;
                        this.testList()
                    }
                    else
                    {
                        this.$message.error(res.data.message);
                    }
                })
            },
            one(scope) {
                this.dialogFormTitle = "{:lang('edit')}"
                http.post(url,{
                    test_id: scope.row.test_id,
                    ccOption: 'one'
                }).then(res => {
                    this.form = res.data.data
                })

                this.dialogFormVisible = true;
            },
            resetForm() {
                this.form = {
                    title: '',
                    test_id: ''
                }
            },
            del(scope) {
                this.$confirm("{:lang('delete_tips')}", "{:lang('delete_title')}", {
                    confirmButtonText: "{:lang('confirm')}",
                    cancelButtonText: "{:lang('cancel')}",
                    type: 'warning'
                }).then(() => {
                    http.post(url,{
                        test_id: scope.row.test_id,
                        ccOption: 'del'
                    }).then(res => {
                        if(res.data.code == 0) {
                            this.$message.success("{:lang('operation_success')}");
                            this.testList()
                        }
                        else
                        {
                            this.$message.error(res.data.message);
                        }
                    })

                }).catch(() => {

                });
            },
            dbclick(row, column, event) {
                //console.log(row.test_id)
            }
        }
    })

</script>
{include file="public/footer" /}